package cn.cs.controller;

import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.EmptyUtils;
import cn.cs.common.IdWorker;
import cn.cs.pojo.ComplaintTitle;
import cn.cs.service.ComplaintTitleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping(value = "/complaintTitle")
public class ComplaintTitleController {

    @Resource
    private ComplaintTitleService complaintTitleService;

    /**
     * 分页查询 投诉主题
     *
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getComplaintTitleByPage")
    public Dto getComplaintTitleByPage(Integer pageNumber, Integer pageSize) throws Exception {
        Map<String, Object> param = new HashMap<>();
        return DtoUtil.returnSuccess("获取成功", complaintTitleService.queryComplaintTitlePageByMap(param, pageNumber, pageSize));
    }

    /**
     * 新增/编辑 投诉主题
     * @param complaintTitle
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateComplaintTitle")
    public Dto updateComplaintTitle(@RequestBody ComplaintTitle complaintTitle) {
        try {
            String msg = "新增成功";
            if (EmptyUtils.isEmpty(complaintTitle.getId())) {
                complaintTitle.setId(IdWorker.getId());
                complaintTitle.setCreateTime(new Date());
                complaintTitle.setModifyTime(new Date());
                complaintTitleService.addComplaintTitle(complaintTitle);
            } else {
                complaintTitle.setModifyTime(new Date());
                complaintTitleService.modifyComplaintTitle(complaintTitle);
                msg = "修改成功";
            }
            return DtoUtil.returnSuccess(msg);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }


    /**
     * 删除 投诉主题 信息
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/delComplaintTitle")
    public Dto delComplaintTitle(String ids)throws Exception{
        int i = 0;
        for(String id:ids.split(",")){
            i += complaintTitleService.deleteComplaintTitleById(id);
        }
        if(i>0){
            return DtoUtil.returnSuccess("操作成功", i);
        }else {
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }

    /**
     * 查询所有 投诉主题
     * @return
     * @throws Exception TODO 添加搜索
     */
    @RequestMapping(value = "/p/getComplaintTitleAll")
    public Dto getComplaintTitleAll(String type) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("type", type);
        return DtoUtil.returnSuccess("获取成功", complaintTitleService.getComplaintTitleListByMap(param));
    }

}
